package com.czk.springboot.reactive.reactor.thread;

import reactor.core.publisher.Flux;
import reactor.core.scheduler.Scheduler;
import reactor.core.scheduler.Schedulers;

import java.io.IOException;

/**
 * @Author: chenzhangkun
 * @CreateTime: 2025-05-19
 * @Description:
 * @Version: 1.0
 */
public class PublishAndSubscribeThreadDemo {

    public static void main(String[] args) throws IOException {

        // 创建线程池
        Scheduler scheduler = Schedulers.newParallel("czk", 4);

        Flux<String> flux = Flux.range(0, 10)
                // 此时使用的是订阅者的线程，因为只有订阅后发布者才有效,此时线程是Thread0
                .map(i -> 10 + i)
                .log()
                .publishOn(scheduler)
                // 产生新流，此时新的发布者线程为指定的线程池,此时线程是czk-1
                .map(i -> "value==" + i)
                .log();
        // 订阅
        new Thread(() -> flux.subscribe(System.out::println)).start();
        System.in.read();

    }


}
